/**
 * 
 */
$(document).ready(function() {
	$("form").submit(function() {
		return false;
	});

	var VAL = {
		EVT_USR : $("#userId").text(),
		T_FDPAPLYWO : "FDPAPLYWO",
		T_FDPAPLYSO : "FDPAPLYSO",
		T_FBPMDLDEF : "FBPMDLDEF",
		T_FBPBISMTR : "FBPBISMTR",
		T_FBPBISCUS : "FBPBISCUS",
		T_FBPBISBOM : "FBPBISBOM",
        T_FBPBISPTH : "FBPBISPTH",
        T_FBPBISDAT : "FBPBISDAT",
		NORMAL : "0000000",
		DISABLED_ATTR : {
			"disabled" : true
		},
		ENABLED_ATTR : {
			"disabled" : false
		}
	};

	var domObj = {
		W : $(window),
		$repairId : $("#repairId"),
		$responsible_usr : $("#responsible_usr"),
		$fab_id_fk : $("#fab_id_fk"),
        $line_id_fk : $("#line_id_fk"),
        $tool_id_fk : $("#tool_id_fk"),
        $assistants : $("#assistants"),
        $tech_confirm_flg : $("#tech_confirm_flg"),
        $qual_confirm_flg : $("#qual_confirm_flg"),
        $prod_confirm_flg : $("#prod_confirm_flg"),
        $downtime : $("#downtime"),
        $error_dsc : $("#error_dsc"),
        $error_analysis : $("#error_analysis"),
        $repair_dsc : $("#repair_dsc"),
        $repair_result : $("#repair_result"),
        $eqt_confirmer : $("#eqt_confirmer"),
        $tech_confirmer : $("#tech_confirmer"),
        $qa_confirmer : $("#qa_confirmer"),
        $pro_confirmer : $("#pro_confirmer"),

		buttons : {
			$query_btn : $("#query_btn"),
			$update_btn : $("#update_btn"),
			$del_btn : $("#del_btn"),
			$save_btn : $("#save_btn"),
			$clear_btn : $("#clear_btn"),
			$comp_btn : $("#comp_btn"),
            $repair_btn : $("#repair_btn"),
            $changeRepair_btn : $("#changeRepair_btn")
		},
        dialog : {
            // $bindMtrlDialog : $("#bindMtrlDialog"),
            // $mtrlCateSel    : $("#mtrlCateSel"),
            //
            // $woMtrlDiv : $("#woMtrlDiv"),
            // $woMtrlGrd : $("#woMtrlGrd"),
            // $woMtrlPg : $("#woMtrlPg"),
            //
            // $bindMtrlDialog_sureBtn : $("#bindMtrlDialog_sureBtn"),
		},
		grids : {
			$repairOrderDiv : $("#repairOrderDiv"),
			$repairOrderGrd : $("#repairOrderGrd"),
	        $UnderDiv         : $(".UnderDiv"),
		}
	};

	function initInnerOrder() {
		domObj.grids.$repairOrderGrd.jqGrid({
			url : "",
			datatype : "local",
			mtype : "POST",
			autowidth : true,
            shrinkToFit:false,
            scroll: true,
			resizable : true,
			loadonce : true,
			viewrecords : true,
			rownumbers : true,
			emptyrecords : true,
			colModel : [ {
				name : 'crt_timestamp',
				index : 'crt_timestamp',
				label : "创建日期",
				width : 170
			},{
                name : 'repair_order_id',
                index : 'repair_order_id',
                label :"维修单号",
                width : 110
            }, {
                name : 'repair_stat',
                index : 'repair_stat',
                label :"维修单状态",
                width : 110
            },{
                name : 'responsible_usr',
                index : 'responsible_usr',
                label :"维修单负责人",
                width : 110
            }, {
				name : 'fab_id_fk',
				index : 'fab_id_fk',
				label : "厂别",
				width : 110
			}, {
				name : 'line_id_fk',
				index : 'line_id_fk',
				label : "线别",
				width : 70
			}, {
				name : 'tool_id_fk',
				index : 'tool_id_fk',
				label : "设备代码",
				width : 110
			},{
                name : 'assistants',
                index : 'assistants',
                label : "协助人员",
                width : 110
            },  {
				name : 'tech_confirm_flg',
				index : 'tech_confirm_flg',
				label : "是否需要工艺确认",
				width : 110
			}, {
                name : 'qual_confirm_flg',
                index : 'qual_confirm_flg',
                label : "是否需要质量确认",
                width : 110
            },{
                name  : 'prod_confirm_flg',
                index : 'prod_confirm_flg',
                label : "是否需要生产确认",
                width : 100
            },{
                name : 'downtime',
                index : 'downtime',
                label : "停机时间",
                width : 100
            },{
                name : 'error_dsc',
                index : 'error_dsc',
                label : "故障描述",
                width : 100
            }, {
				name : 'error_analysis',
				index : 'error_analysis',
				label : "故障原因分析",
				width : 110
			}, {
				name : 'repair_dsc',
				index : 'repair_dsc',
				label : "维修内容描述",
				width : 110
			}, {
                name : 'repair_result',
                index : 'repair_result',
                label : "维修后确认项目及结果",
                width : 100
            }, {
                name : 'eqt_confirmer',
                index : 'eqt_confirmer',
                label : "设备确认人",
                width : 100
            }, {
                name : 'tech_confirmer',
                index : 'tech_confirmer',
                label : "工艺确认人",
                width : 100
            }, {
                name : 'qa_confirmer',
                index : 'qa_confirmer',
                label : "质量确认人",
                width : 100
            }, {
                name : 'pro_confirmer',
                index : 'pro_confirmer',
                label : "生产确认人",
                width : 100
            }],
			pager : "repairOrderPg",
            onSelectRow : function(id) {
               var  selRowId = domObj.grids.$repairOrderGrd.jqGrid("getGridParam", "selrow");
                var  rowData = domObj.grids.$repairOrderGrd.jqGrid("getRowData",selRowId);
                domObj.$repairId.val(rowData.repair_order_id);
                domObj.$responsible_usr.val(rowData.responsible_usr);
                SelectDom.setSelect($("#fab_id_fk"),rowData.fab_id_fk,rowData.fab_id_fk);
                SelectDom.setSelect($("#line_id_fk"),rowData.line_id_fk,rowData.line_id_fk);
                SelectDom.setSelect($("#tool_id_fk"),rowData.tool_id_fk,rowData.tool_id_fk);
                domObj.$assistants.val(rowData.assistants);
                SelectDom.setSelect($("#tech_confirm_flg"),rowData.tech_confirm_flg,rowData.tech_confirm_flg);
                SelectDom.setSelect($("#qual_confirm_flg"),rowData.qual_confirm_flg,rowData.qual_confirm_flg);
                SelectDom.setSelect($("#prod_confirm_flg"),rowData.prod_confirm_flg,rowData.prod_confirm_flg);
                domObj.$downtime.val(rowData.downtime);
                domObj.$error_dsc.val(rowData.error_dsc);
                domObj.$error_analysis.val(rowData.error_analysis);
                domObj.$repair_dsc.val(rowData.repair_dsc);
                domObj.$repair_result.val(rowData.repair_result);
                domObj.$eqt_confirmer.val(rowData.eqt_confirmer);
                domObj.$tech_confirmer.val(rowData.tech_confirmer);
                domObj.$qa_confirmer.val(rowData.qa_confirmer);
                domObj.$pro_confirmer.val(rowData.pro_confirmer);

            }
		});
	};

	// 初始化下拉框
	var initSelectFnc = {
		//初始化厂别
        initFabIdSel : function (){
            comAddValueByDataCateFnc("#fab_id_fk","FBID","data_ext","data_desc",true);
        },

		//初始化线别
        initLineSel : function (){
            comAddValueByDataCateFnc("#line_id_fk","AREA","data_ext","data_desc",true);
        },

        //初始化设备
        iniToolSel: function(){
            var fabId =  $.trim($("#fab_id_fk").val());
            var lineId =   $.trim($("#line_id_fk").val());
            // var opeId = $.trim($("#opeIdSel").val());
            // var opeVer = '00000';
            var iary2  = {
                fab_id_fk : fabId,
                bay_id :lineId,
                // ope_id_fk : opeId,
                // ope_ver : opeVer
            }
            var inObj2 = {
                trx_id : 'FBPBISTOL',
                action_flg : "Q",
                iary : [iary2]
            };
            var outObj2 = comTrxSubSendPostJson(inObj2);
            if(outObj2.rtn_code === "0000000"){
                SelectDom.initWithSpace($("#tool_id_fk"));
                SelectDom.addSelectArr($("#tool_id_fk"),outObj2.oary,["tool_id"],["tool_dsc"],"Y",true);
            }
        },

		//是否需要工艺确认
        initTechConfirmFlg : function () {
            $("#tech_confirm_flg").empty();
            $("#tech_confirm_flg").append("<option value="+"Y"+">"+"Y" +"</option>");
            $("#tech_confirm_flg").append("<option value="+"N"+">"+"N" +"</option>");
            $("#tech_confirm_flg").select2({
                theme : "bootstrap"
            });
        },

		changeTechInput : function(){
        	var tech_confirm_flg = domObj.$tech_confirm_flg.val();
        	if(tech_confirm_flg == "Y"){
        		domObj.$tech_confirmer.attr(VAL.ENABLED_ATTR);
			}else{
                domObj.$tech_confirmer.val("");
                domObj.$tech_confirmer.attr(VAL.DISABLED_ATTR);
			}
		},

        changeQaInput : function(){
            var qa_confirm_flg = domObj.$qual_confirm_flg.val();
            if(qa_confirm_flg == "Y"){
                domObj.$qa_confirmer.attr(VAL.ENABLED_ATTR);
            }else{
                domObj.$qa_confirmer.val("");
                domObj.$qa_confirmer.attr(VAL.DISABLED_ATTR);
            }
        },

        changeProInput : function(){
            var pro_confirm_flg = domObj.$prod_confirm_flg.val();
            if(pro_confirm_flg == "Y"){
                domObj.$pro_confirmer.attr(VAL.ENABLED_ATTR);
            }else{
                domObj.$pro_confirmer.val("");
                domObj.$pro_confirmer.attr(VAL.DISABLED_ATTR);
            }
        },

		//是否需要质量确认
        initQaConfirmFlg : function () {
            $("#qual_confirm_flg").empty();
            $("#qual_confirm_flg").append("<option value="+"Y"+">"+"Y" +"</option>");
            $("#qual_confirm_flg").append("<option value="+"N"+">"+"N" +"</option>");
            $("#qual_confirm_flg").select2({
                theme : "bootstrap"
            });
        },

		//是否需要生产确认
        initProdConfirmFlg : function () {
            $("#prod_confirm_flg").empty();
            $("#prod_confirm_flg").append("<option value="+"Y"+">"+"Y" +"</option>");
            $("#prod_confirm_flg").append("<option value="+"N"+">"+"N" +"</option>");
            $("#prod_confirm_flg").select2({
                theme : "bootstrap"
            });
        },



	};

	function init() {
		initSelectFnc.initFabIdSel();
		initSelectFnc.initLineSel();
		initSelectFnc.iniToolSel();
		initSelectFnc.initTechConfirmFlg();
        initSelectFnc.initQaConfirmFlg();
        initSelectFnc.initProdConfirmFlg();
	};


	var buttonFnc = {
		queryFnc : function() {
			var repairId = domObj.$repairId.val();
			var responsible_usr = domObj.$responsible_usr.val();
			var fab_id_fk = domObj.$fab_id_fk.val();
			var line_id_fk = domObj.$line_id_fk.val();
			var tool_id_fk = domObj.$tool_id_fk.val();

			var inObj = {
				trx_id : "FDPAPLYRE",
				action_flg : "Q",
                repair_order_id : repairId,
                responsible_usr : responsible_usr,
                fab_id_fk : fab_id_fk,
                line_id_fk : line_id_fk,
                tool_id_fk : tool_id_fk
			};
			var outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				setGridInfo(outObj.oary, "#repairOrderGrd");
			}
		},
		//更新或者新增
		saveFnc : function() {
			var actionFlg;
			// 可编辑说明为新增，不可编辑说明为修改
			actionFlg = domObj.$tool_id_fk.attr("disabled") === "disabled"  ? (domObj.$repair_result.attr("disabled") === "disabled" ? "Z" : "U")  : "A";
            var repairId = domObj.$repairId.val();
            var responsible_usr = domObj.$responsible_usr.val();
            var fab_id_fk = domObj.$fab_id_fk.val();
            var line_id_fk = domObj.$line_id_fk.val();
            var tool_id_fk = domObj.$tool_id_fk.val();

			var assistants = domObj.$assistants.val();
			var tech_confirm_flg = domObj.$tech_confirm_flg.val();
			var qual_confirm_flg = domObj.$qual_confirm_flg.val();
			var prod_confirm_flg = domObj.$prod_confirm_flg.val();
			var downtime = domObj.$downtime.val();
			var error_dsc = domObj.$error_dsc.val();
			var error_analysis = domObj.$error_analysis.val();
			var repair_dsc = domObj.$repair_dsc.val();
			var repair_result = domObj.$repair_result.val();
			var eqt_confirmer = domObj.$eqt_confirmer.val();
			var tech_confirmer = domObj.$tech_confirmer.val();
			var qa_confirmer = domObj.$qa_confirmer.val();
			var pro_confirmer = domObj.$pro_confirmer.val();


			// 输给后台的数据
			var iaryA =[];
			var iary={
                assistants : assistants,
                tech_confirm_flg : tech_confirm_flg,
                qual_confirm_flg : qual_confirm_flg,
                prod_confirm_flg : prod_confirm_flg,
                downtime : downtime,
                error_dsc : error_dsc,
                error_analysis : error_analysis,
                repair_dsc : repair_dsc,
                repair_result : repair_result,
                eqt_confirmer : eqt_confirmer,
                tech_confirmer : tech_confirmer,
                qa_confirmer : qa_confirmer,
                pro_confirmer : pro_confirmer,
			}
            iaryA.push(iary);
			var inObj = {
				trx_id       : "FDPAPLYRE",
				action_flg  : actionFlg,
				opi_ope_flg : 'Y',
				evt_usr      : VAL.EVT_USR,
                repair_order_id      : repairId,
                responsible_usr      : responsible_usr,
                fab_id_fk      : fab_id_fk,
                line_id_fk      : line_id_fk,
                tool_id_fk      : tool_id_fk,
				iary         : iaryA
			};
			var outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code === VAL.NORMAL) {
				if (actionFlg == "A") {
					var newRowId = getGridNewRowID("#repairOrderGrd");
					domObj.grids.$repairOrderGrd.jqGrid("addRowData", newRowId,outObj.oary[0]);
					domObj.grids.$repairOrderGrd.setSelection(newRowId, true);
					showSuccessDialog("维修单新建成功");
					// $("#update_btn").attr(VAL.ENABLED_ATTR);
				} else if (actionFlg == "U") {
					var selRowId = domObj.grids.$repairOrderGrd.jqGrid("getGridParam", "selrow");
					domObj.grids.$repairOrderGrd.jqGrid("setRowData", selRowId,outObj.oary[0]);
					showSuccessDialog("维修单更新成功");
				} else if (actionFlg == "Z"){
                    var selRowId = domObj.grids.$repairOrderGrd.jqGrid("getGridParam", "selrow");
                    domObj.grids.$repairOrderGrd.jqGrid("setRowData", selRowId,outObj.oary[0]);
                    showSuccessDialog("维修单转单成功");
                }
			}
		},
        //维修单转单
        changeFnc: function(){
            var selRowId, rowData, iary, inObj, outObj, woStat;

            selRowId = domObj.grids.$repairOrderGrd.jqGrid("getGridParam", "selrow");
            if (!selRowId) {
                showErrorDialog("", "请选择需要修改的维修单");
                return false;
            }
            unableFn1();
        },

		updateFnc : function() {
			var selRowId, rowData, iary, inObj, outObj, woStat;

			selRowId = domObj.grids.$repairOrderGrd.jqGrid("getGridParam", "selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要修改的维修单");
				return false;
			}
            unableFn();
		},

		delFnc : function() {
			var selRowId, rowData, iary, inObj, outObj;
			// 获得选中的行ID
			selRowId = domObj.grids.$repairOrderGrd.jqGrid("getGridParam", "selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要删除的维修单");
				return false;
			}
			// 删除按钮绑定删除事件
			$("#del_btn").showCallBackWarnningDialog({
				errMsg : "是否删除该维修单信息,请确认!!!!",
				callbackFn : function(data) {
					if (data.result == true) {
						// 获得选中行的数据
						rowData = domObj.grids.$repairOrderGrd.jqGrid("getRowData",selRowId);
						inObj = {
							trx_id : "FDPAPLYRE",
							action_flg : "D",
							repair_order_id : rowData.repair_order_id,
                            evt_usr      : VAL.EVT_USR
						};
						outObj = comTrxSubSendPostJson(inObj);
						if (outObj.rtn_code === VAL.NORMAL) {
							showSuccessDialog("删除工单成功");
							domObj.grids.$repairOrderGrd.jqGrid("delRowData",selRowId);
						}
					}
				}
			});
		},


		clearFnc : function() {
			enableFn();
			SelectDom.initWithSpace(domObj.$fab_id_fk);
			SelectDom.initWithSpace(domObj.$line_id_fk);
			SelectDom.initWithSpace(domObj.$tool_id_fk);
			SelectDom.initWithSpace(domObj.$tech_confirm_flg);
			SelectDom.initWithSpace(domObj.$qual_confirm_flg);
			SelectDom.initWithSpace(domObj.$prod_confirm_flg);
			$("input").val("");
			init();
			$("#repairOrderGrd").jqGrid('clearGridData');
		},
		
		repairFnc : function () {
			var repairId = domObj.$repairId.val();
            var inObj = {
                trx_id : "FDPAPLYRE",
                action_flg : "R",
                repair_order_id : repairId,
                evt_usr      : VAL.EVT_USR
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                buttonFnc.queryFnc();
                showSuccessDialog("开始维修！");
            }
        },
		
		compFnc : function () {
            var repairId = domObj.$repairId.val();
            var inObj = {
                trx_id : "FDPAPLYRE",
                action_flg : "C",
                repair_order_id : repairId,
                evt_usr      : VAL.EVT_USR
            };
            var outObj = comTrxSubSendPostJson(inObj);
            if (outObj.rtn_code === VAL.NORMAL) {
                buttonFnc.queryFnc();
                showSuccessDialog("维修完成！");
            }
        }

	};

    var iniButtonAction = function(){
        domObj.buttons.$query_btn.bind('click',buttonFnc.queryFnc);
        domObj.buttons.$save_btn.bind('click',buttonFnc.saveFnc);
        domObj.buttons.$update_btn.bind('click',buttonFnc.updateFnc);
        domObj.buttons.$del_btn.bind('click', buttonFnc.delFnc);
        domObj.buttons.$clear_btn.bind('click',buttonFnc.clearFnc);
        domObj.buttons.$repair_btn.bind('click',buttonFnc.repairFnc);
        domObj.buttons.$comp_btn.bind('click',buttonFnc.compFnc);
        domObj.buttons.$changeRepair_btn.bind('click',buttonFnc.changeFnc);
    };

    var otherActionBind = function(){
        $("#fab_id_fk").on("change",initSelectFnc.iniToolSel);
        $("#line_id_fk").on("change",initSelectFnc.iniToolSel);
        domObj.$tech_confirm_flg.on("change",initSelectFnc.changeTechInput);
        domObj.$qual_confirm_flg.on("change",initSelectFnc.changeQaInput);
        domObj.$prod_confirm_flg.on("change",initSelectFnc.changeProInput);
	};

    //jqGrid表格自适应
    function resizeFnc(){
        var grdDivHeight,grdDivWidth;
        grdDivWidth = domObj.W.width() - 60;
        grdDivHeight = domObj.W.height() - domObj.grids.$UnderDiv.offset().top;

        domObj.grids.$UnderDiv.height(grdDivHeight);
        domObj.grids.$repairOrderDiv.width(grdDivWidth);
        domObj.grids.$repairOrderDiv.height(grdDivHeight*0.99 - 51);
        domObj.grids.$repairOrderGrd.setGridWidth(grdDivWidth*0.99);
        domObj.grids.$repairOrderGrd.setGridHeight(grdDivHeight*0.95 - 101);
    };
    domObj.W.resize(function() {
        resizeFnc();
    });

    //转工单时控件
    function unableFn1(){
        domObj.$repairId.attr(VAL.DISABLED_ATTR);
        domObj.$fab_id_fk.attr(VAL.DISABLED_ATTR);
        domObj.$line_id_fk.attr(VAL.DISABLED_ATTR);
        domObj.$tool_id_fk.attr(VAL.DISABLED_ATTR);
        domObj.$tech_confirm_flg.attr(VAL.DISABLED_ATTR);
        domObj.$qual_confirm_flg.attr(VAL.DISABLED_ATTR);
        domObj.$prod_confirm_flg.attr(VAL.DISABLED_ATTR);
        domObj.$downtime.attr(VAL.DISABLED_ATTR);
        domObj.$error_analysis.attr(VAL.DISABLED_ATTR);
        domObj.$repair_dsc.attr(VAL.DISABLED_ATTR);
        domObj.$repair_result.attr(VAL.DISABLED_ATTR);
        domObj.$eqt_confirmer.attr(VAL.DISABLED_ATTR);
        domObj.$tech_confirmer.attr(VAL.DISABLED_ATTR);
        domObj.$qa_confirmer.attr(VAL.DISABLED_ATTR);
        domObj.$pro_confirmer.attr(VAL.DISABLED_ATTR);
        domObj.$error_dsc.attr(VAL.DISABLED_ATTR);
        domObj.$assistants.attr(VAL.DISABLED_ATTR);
    }
    //将控件变成不可修改
    function unableFn() {
        domObj.$repairId.attr(VAL.DISABLED_ATTR);
        domObj.$fab_id_fk.attr(VAL.DISABLED_ATTR);
        domObj.$line_id_fk.attr(VAL.DISABLED_ATTR);
        domObj.$tool_id_fk.attr(VAL.DISABLED_ATTR);
    }

    //将控件变成都可修改
    function enableFn() {
        domObj.$repairId.attr(VAL.ENABLED_ATTR);
        domObj.$responsible_usr.attr(VAL.ENABLED_ATTR);
        domObj.$fab_id_fk.attr(VAL.ENABLED_ATTR);
        domObj.$line_id_fk.attr(VAL.ENABLED_ATTR);
        domObj.$tool_id_fk.attr(VAL.ENABLED_ATTR);
    }

	//页面初始化函数
	function pageInit() {
		initInnerOrder();
		resizeFnc();
		init();
		enableFn();
        iniButtonAction();
        otherActionBind();
	}
	pageInit();

});
